package com.org.cys.controller.information;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.org.cys.controller.base.BaseController;
import com.org.cys.pojo.base.Const;
import com.org.cys.pojo.base.Page;
import com.org.cys.pojo.information.data.NewsEnum;
import com.org.cys.service.information.NewsService;
import com.org.cys.util.ExportExcel;
import com.org.cys.util.MapUtil;
import com.org.cys.util.PageData;
import com.org.cys.util.StringUtil;  
  

/**
 * 新闻信息Controller
 * @author caiyushen
 * */
@Controller  
@RequestMapping("/news")  
public class NewsController extends BaseController{  
    
	private static Logger logger = Logger.getLogger(NewsController.class);
	
	@Resource(name="newsServiceImpl")
    private NewsService newsService;  
	
   /*================================================admin/news/list.jsp  start=================================================*/
	
   /**
     * 去用户列表页面
     * @author caiyushen
     * */
    @RequestMapping(value="/toList",method = RequestMethod.GET)  
    public String toList() throws Exception{  
        return "news/list";  
    }  
	
	
    /**
     * 获取列表信息
     * @param page 分页实体类
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(value="/list",method = RequestMethod.GET)
    public PageData list(Page page){
    	PageData pd = new PageData();
    	try{
    		pd = this.getPageData();
    		page.setPd(pd);
    		logger.info("method list() param: " + page.toString());
    		List<PageData> dataList = newsService.dataList(page);
    		NewsEnum.convertColor(dataList);
    		pd.put("dataList", dataList);
    		pd.put("total",page.getTotalResult());
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    	}
    	return pd;
    }
    
    /**
     * 删除News
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE)
    public PageData delete(@RequestBody PageData pd){
    	logger.info("method delete() param: "+pd.toString());
    	PageData result = new PageData();
    	try{
    		newsService.delete(pd);
    		result.put("success", true);
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    		result.put("success", false);
    	}
    	return result;
    }
    
    /**
     * 导出Excel数据
     * */
    @RequestMapping(value="/exportExcel",method = RequestMethod.GET)
    public ModelAndView exportExcel(){
    	ModelAndView mv = new ModelAndView();
    	PageData pd = new PageData();
    	pd = this.getPageData();
    	try{
    		Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
		    titles.add("新闻标题"); //2	
		    titles.add("新闻文本内容"); //3	
		    titles.add("新闻html内容"); //4	
		    titles.add("新闻状态"); //5	
		    titles.add("创建时间"); //6	
		    titles.add("修改时间"); //7	
			dataMap.put("titles", titles);
			List<PageData> varOList = newsService.listAll(pd);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<varOList.size();i++){
				PageData vpd = new PageData();
				vpd.put("var2", varOList.get(i).getString("newsTitle"));//2
				vpd.put("var3", varOList.get(i).getString("newsTxtContent"));//3
				vpd.put("var4", varOList.get(i).getString("newsHtmlContent"));//4
				vpd.put("var5", varOList.get(i).getString("newsStatus"));//5
				vpd.put("var6", varOList.get(i).getString("createTime"));//6
				vpd.put("var7", varOList.get(i).getString("modifiedTime"));//7
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ExportExcel erv = new ExportExcel();
			mv = new ModelAndView(erv,dataMap);
    	}catch(Exception e){
			logger.error(e.toString(), e);
		}
    	
    	return mv;
    }
    
    /**
     * 批量修改新闻状态
     * @param pd-newsStatus 新闻状态 1:已发布  2:未发布
     * @param pd-ArrayDATA_IDS id数组
     * */
    @ResponseBody
    @RequestMapping(value="updateNewsStatus",method = RequestMethod.PUT)
    public PageData updateNewsStatus(@RequestBody PageData pd){
    	logger.info("method updateNewsStatus() param: "+pd.toString());
    	String DATA_IDS = MapUtil.getString(pd, "DATA_IDS");
    	PageData result = new PageData();
    	if(StringUtil.checkObj(DATA_IDS)){
    		try{
    			String ArrayDATA_IDS[] = DATA_IDS.split(",");
        		newsService.updateNewsStatus(ArrayDATA_IDS,MapUtil.getInteger(pd, "newsStatus"));
        		result.put("success", true);
        		return result;
        	}catch(Exception e){
        		logger.error(e.toString(),e);
        		result.put("success", false);
        		result.put("error", "服务器发生异常！");
        		return result;
        	}
    	}
    	result.put("success", false);
    	result.put("error", "新闻数据不能为空值！");
    	return result;
    }
    
    
    /*================================================admin/news/list.jsp  end=================================================*/
    
    
    /*================================================admin/news/edit.jsp  start==================================================*/
    
    /**
     * 跳转到新增News页面
     * @author caiyushen
     * */
    @RequestMapping(value="toAdd",method = RequestMethod.GET)
    public String toAdd() throws Exception{  
        return "news/edit";  
    }  
    
    
    /**
     * 跳转到编辑News页面
     * @param pd id 新闻id
     * @author caiyushen
     * */
    @RequestMapping(value="toEdit",method = RequestMethod.GET)
    public String toEdit(@RequestParam Map<String,Object> param,Model model) throws Exception{
    	logger.info("method toEdit param:"+param.toString());
    	PageData news = newsService.getEntityById(MapUtil.getLong(param, "id"));
    	model.addAttribute("news",news);
        return "news/edit";  
    }  
    
    /**
     * 新增News
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public PageData add(@RequestBody PageData pd){
    	logger.info("method add() param: "+pd.toString());
    	PageData result = new PageData();
    	try{
    		//shiro管理的session
    		Subject currentUser = SecurityUtils.getSubject();  
    		Session session = currentUser.getSession();
    		Long userId = (Long)session.getAttribute(Const.sessionUserId);
    		pd.put("createUserId", userId);
    		newsService.save(pd);
    		result.put("success", true);
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    		result.put("success", false);
    	}
    	return result;
    }
   
    
    /**
     * 修改News
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public PageData update(@RequestBody PageData pd){
    	logger.info("method update() param: "+pd.toString());
    	PageData result = new PageData();
    	try{
    		newsService.update(pd);
    		result.put("success", true);
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    		result.put("success", false);
    	}
    	return result;
    }
    
    /*================================================admin/news/edit.jsp  end==================================================*/
    
}  